from typing import Dict, List
import logging


class QueueManager:
    """
    队列管理类，负责管理脚本执行队列
    """

    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._init_queue()
        return cls._instance

    def _init_queue(self):
        """初始化队列"""
        self.queue: Dict[str, Dict] = {}
        self.logger = logging.getLogger(__name__)

    @classmethod
    def get_all_items(cls) -> List[Dict]:
        """
        获取队列中所有项目
        
        Returns:
            List[Dict]: 队列中所有项目的列表
        """
        instance = cls()
        return list(instance.queue.values())

    def add_item(self, item_id: str, item_data: Dict):
        """
        添加项目到队列
        
        Args:
            item_id (str): 项目ID
            item_data (Dict): 项目数据
        """
        self.queue[item_id] = item_data
        self.logger.info(f"已添加项目到队列: {item_id}")

    def remove_item(self, item_id: str):
        """
        从队列中移除项目
        
        Args:
            item_id (str): 项目ID
        """
        if item_id in self.queue:
            del self.queue[item_id]
            self.logger.info(f"已从队列中移除项目: {item_id}")

    def get_item_status(self, item_id: str) -> str:
        """
        获取项目状态
        
        Args:
            item_id (str): 项目ID
        
        Returns:
            str: 项目状态
        """
        return self.queue.get(item_id, {}).get("status", "未在队列")